//
//  TimestampHelper.m
//  CLExtension
//
//  Created by CharlieLiao on 2017/8/9.
//  Copyright © 2017年 Charlie. All rights reserved.
//
#import "CountDownHelper.h"
#import "NSString+CLExtension.h"

@implementation CountDownHelper

/// 将TimeInterval格式转换为时间字符串，如“00:01:55”
+ (NSString *)countdown:(NSTimeInterval)interval
{
    long long hour = 0;
    long long minute = 0;
    long long second = 0;
    
    BOOL isPositive = (interval >= 0);
    long long timeInt = isPositive? interval : -interval;
    
    if (isPositive) {
        hour = timeInt/3600;
        minute = (timeInt%3600)/60;
        second = (timeInt%3600)%60;
    }

    //    long long hour = timeInt/3600;
    //    long long minute = (timeInt%3600)/60;
    //    long long second = (timeInt%3600)%60;
    
    //    NSString *timeString = isPositive? @"" : @"-";
    
    //    timeString = [timeString stringByAppendingFormat:@"%02lld:%02lld:%02lld", hour, minute, second];
    //    return timeString;
    return [NSString stringWithFormat:@"%02lld:%02lld:%02lld", hour, minute, second];
}

/// 如：将“00:01:55”转成NSTimeInterval格式
+ (NSTimeInterval)timeIntervalWithCountdown:(NSString *)timeString
{
    NSArray<NSString*>* sections = [timeString componentsSeparatedByString:@":"];
    // 秒
    if (sections.lastObject.isFloat) {
        NSTimeInterval interval = sections.lastObject.floatValue;
        
        NSUInteger count = sections.count;
        // 分
        if (count > 1) {
            NSString *minuteStr = sections[count-2];
            if (minuteStr.isInt) {
                interval += minuteStr.intValue*60;
                
                // 时
                if (count > 2) {
                    NSString *hourStr = sections[count-3];
                    if (hourStr.isInt) {
                        interval += hourStr.intValue*3600;
                    }
                }
            }
        }
        return interval;
    }
    return 0;
}


/// 两个时间字符串相加，返回累加后的时间字符串
+ (NSString *)additionWithCountDown:(NSString *)first second:(NSString *)second
{
    NSTimeInterval fInterval = [self timeIntervalWithCountdown:first];
    NSTimeInterval sInterval = [self timeIntervalWithCountdown:second];
    return [self countdown:fInterval+sInterval];
}

@end










//// 时间分段方法（今天、昨天、MM月dd日、yyyy年MM月dd日）
//+ (NSString *)timeSegement:(NSDate *)cTime{
//
//    // yyyy-MM-dd  HH:mm:ss
//    NSTimeInterval now = [[NSDate date]timeIntervalSince1970];
//    NSTimeInterval beTime = [cTime timeIntervalSince1970];
//    double distanceTime = fabs(now - beTime);
//    NSString *distanceStr;
//
//    NSDate *beDate = [NSDate dateWithTimeIntervalSince1970:beTime];
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//
//    [dateFormatter setDateFormat:@"HH:mm"];
//    //    NSString *timeStr = [dateFormatter stringFromDate:beDate];
//
//    [dateFormatter setDateFormat:@"dd"];
//    NSString *nowDay = [dateFormatter stringFromDate:[NSDate date]];
//    NSString *lastDay = [dateFormatter stringFromDate:beDate];
//
//    [dateFormatter setDateFormat:@"yyyy"];
//    NSString *nowYear = [dateFormatter stringFromDate:[NSDate date]];
//    NSString *beYear = [dateFormatter stringFromDate:beDate];
//
//    if (distanceTime < 60) {
//        distanceStr = Lang(@"今天");
//    }
//    else if (distanceTime <60*60 || (distanceTime < 24*60*60 && [nowDay integerValue] == [lastDay integerValue])){
//
//        //        distanceStr = timeStr;
//        distanceStr = Lang(@"今天");
//    }
//    else if (distanceTime < 24*60*60*2 && [nowDay integerValue] != [lastDay integerValue]){
//
//        if ([nowDay integerValue] - [lastDay integerValue] ==1 || ([lastDay integerValue] - [nowDay integerValue] > 10 && [nowDay integerValue] == 1)) {
//            distanceStr = Lang(@"昨天");
//        }else{
//
//            [dateFormatter setDateFormat:Lang(@"MM.dd")];
//            distanceStr = [dateFormatter stringFromDate:beDate];
//
//        }
//
//    }else {
//        // 判断是不是同一年，不是同一年把年份显示
//        if ([nowYear integerValue] == [beYear integerValue]) {
//
//            [dateFormatter setDateFormat:Lang(@"MM.dd")];
//            distanceStr = [dateFormatter stringFromDate:beDate];
//
//        }else{
//
//            [dateFormatter setDateFormat:Lang(@"yyyy.MM.dd")];
//            distanceStr = [dateFormatter stringFromDate:beDate];
//
//        }
//    }
//    return distanceStr;
//}
